var ProtoBufUtil = require('ProtoBufUtil');
var QQHttp = require('QQHttp');
var NativeHttp = require('NativeHttp');

window.HttpUtil = (function(){


    let _static = {
        ip: "http://127.0.0.1:8085",
        errorCode:null,

        setIp:function(ip){
            this.ip = ip;
        },

        setErrorCode:function(errorCode){
            this.errorCode = errorCode;
        },


        /**
         * by     黄新宇
         * proto  get请求方式     最佳适配：nafos服务端
         * @param returnProtoUrl  接受返回值的protoUrl
         * @param returnProtoName  接受返回值的Message
         * @param uri              请求路径
         * @param callback         回调
         */
        nafosBinaryGet: function ( returnProtoUrl, returnProtoName, uri, callback) {
            let self = this;
            if (!uri || uri == '') {
                return;
            }
            let  url = this.ip + uri;
            UIHelper.showLoaing();
            if (SystemInfo.getPlatform() == 'qqPlay') { //QQ渠道
                QQHttp.get(url,function(isTimeOut, obj){
                    self.qReturnCallBack(url,returnProtoUrl, returnProtoName,isTimeOut, obj,callback)
                });
                return;
            }

            /**
             * 中间扩充其他特殊API渠道。记得return
             */

             // 最后是JS原生渠道，能用XHR的
            NativeHttp.get('arraybuffer', url, function (isTimeOut, xhr) {
                self.returnCallBack(url,returnProtoUrl, returnProtoName,isTimeOut, xhr,callback)
                });
        },


        nafosJsonGet: function ( uri, callback) {
            var xhr = new XMLHttpRequest();
            xhr.onreadystatechange = function () {
                if (xhr.readyState == 4 && (xhr.status >= 200 && xhr.status < 400)) {
                    var response = xhr.responseText;
                    Log.info('httpGet' + response);
                    callback(JSON.parse(response));
                }
            };
            xhr.open('GET', this.ip + uri);
            xhr.send();
        },



        nafosBinaryPost: function (uri, param, sendProtoUrl,sendProtoName, returnProtoUrl, returnProtoName,callback) { // 地址 , 结果返回
            let newUrl = this.ip + uri;
            let self = this;
            UIHelper.showLoaing();

            ProtoBufUtil.protoEncode(sendProtoUrl, sendProtoName, param, function (buffer) {
                //QQ post
                if (SystemInfo.getPlatform() == 'qqPlay') { //QQ渠道
                    QQHttp.post(newUrl, buffer,function(isTimeOut, obj){
                        self.qReturnCallBack(newUrl,returnProtoUrl, returnProtoName,isTimeOut, obj,callback)
                    });
                    return;
                }


                //原生
                NativeHttp.post('arraybuffer', newUrl, buffer, function (isTimeOut, xhr) {
                    self.returnCallBack(newUrl,returnProtoUrl, returnProtoName,isTimeOut, xhr,callback)
                });

            })
        },


        /**
         * 消息返回统一处理
         * @param isTimeOut
         * @param xhr
         * @param callBack
         */
        returnCallBack:function(url,returnProtoUrl, returnProtoName,isTimeOut, xhr,callback){
            if(isTimeOut){
                this.doTimeOut(url);
                return;
            }
            if (xhr&&xhr.response&&returnProtoUrl&&returnProtoName) {
                ProtoBufUtil.protoDecode( returnProtoUrl, returnProtoName,xhr.response, callback);
            }
        },
        qReturnCallBack:function(url,returnProtoUrl, returnProtoName,isTimeOut, obj,callback){
            if(isTimeOut){
                self.doTimeOut(url);
                return;
            }
            if(obj&& obj.arrayBuffer()&&returnProtoUrl&&returnProtoName){
                let body = obj.arrayBuffer();
                ProtoBufUtil.protoDecode(returnProtoUrl, returnProtoName, body, callback);
            }
        },


        /**
         * 请求超时处理
         * @param url
         */
        doTimeOut: function (url) {
            Log.error('--------------请求超时：', url);
            UIHelper.hideLoaing();
            let tipsMsg = '网络连接超时';
            // AddPrefabHelper.addMsgTipsUI(tipsMsg, true);
        },


        /**
         * 错误码
         * @param status
         * @returns {string}
         */
        getTextWithStatus: function (status) {
            status = status.toString();
            return this.errorCode[status]?this.errorCode[status]:"系统异常";
        },

    }
    return _static;
})();

module.exports = HttpUtil;